package my.hw;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private Button btnRestart;
	
	private Board board;
	
	private GameStatus gameStatus;
	
	private AbstractRule<ChessColor> rule_game= new AbstractRule<ChessColor>() {
		@Override
		protected boolean isReverseColor(Point pos, ChessColor color) {
			if (board.findGrid(pos).getChess().getColor()!=color) return true;
			return false;
		}

		@Override
		protected boolean isEmpty(Point pos) {
			return board.findGrid(pos).getChess()==null;
		}

		@Override
		protected boolean isLegal(Point pos) {
			return board.findGrid(pos)!=null;
		}
	};
	
	private final OnClickListener 
	btnRestart_Click = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			for (Grid grid : board.getGrids()) {
				grid.setChess(null);
				grid.resetColor();
			}
			board.findGrid(new Point(3,3)).setChess(new Chess(ChessColor.white));
			board.findGrid(new Point(3,4)).setChess(new Chess(ChessColor.black));
			board.findGrid(new Point(4,3)).setChess(new Chess(ChessColor.black));
			board.findGrid(new Point(4,4)).setChess(new Chess(ChessColor.white));
			
			gameStatus.setCurrentColor(ChessColor.white);
		}
	};
	
	private void board_Click(Grid grid) {
		
		
		{
			Point position= grid.getPosition();
			Point[] result = rule_game.judge(gameStatus.getCurrentColor(), position);
			
			if(result.length==0){
				return;
			}
			
			for (Point pos : result) {
			    board.findGrid(pos).setChess(new Chess(gameStatus.getCurrentColor()));
			}
			
			board.findGrid(position).setChess(new Chess(gameStatus.getCurrentColor()));
		}
		
		gameStatus.change();
		
		{
			boolean able = false;
			for (Grid grid_tmp : board.getGrids()) {
				Point[] result = rule_game.judge(gameStatus.getCurrentColor(), grid_tmp.getPosition());
				if(result.length!=0) {
				    grid_tmp.resetColor(gameStatus.getCurrentColor()==ChessColor.white? Color.WHITE : Color.BLACK);
					able =true;
				} else {
				    grid_tmp.resetColor();
				}
			}
			if(able){
				return;
			}
		}
		
	
		gameStatus.change();
		
		{
			boolean able = false;
			for (Grid grid_tmp : board.getGrids()) {
				Point[] result = rule_game.judge(gameStatus.getCurrentColor(), grid_tmp.getPosition());
				if(result.length!=0) {
					grid_tmp.resetColor(gameStatus.getCurrentColor()==ChessColor.white? Color.WHITE : Color.BLACK);
					able =true;
					break;
				}
			}
			if(able){
				return;
			}
		}
		
		{
			int whiteCount =0;
			int blackCount =0;
			
			for (Grid grid_tmp : board.getGrids()) {
				if(grid_tmp.getChess().getColor()==ChessColor.white)
					whiteCount++;
				if(grid_tmp.getChess().getColor()==ChessColor.black)
					blackCount++;
			}
			
			if(whiteCount > blackCount){
				Toast.makeText(MainActivity.this, "白子贏了:"+whiteCount +":"+blackCount, Toast.LENGTH_LONG).show();
			}else if(whiteCount < blackCount){
				Toast.makeText(MainActivity.this, "黑子贏了:"+whiteCount +":"+blackCount, Toast.LENGTH_LONG).show();
			}else /*if(whiteCount == blackCount)*/{
				Toast.makeText(MainActivity.this, "平手:"+whiteCount +":"+blackCount, Toast.LENGTH_LONG).show();
			}
		}	
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		board= new Board(this);
		for (final Grid grid : board.getGrids()) {
			grid.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					board_Click(grid);
				}
			});
		}
		
		gameStatus = new GameStatus();
		
		btnRestart= (Button) findViewById(R.id.btnReset);
		btnRestart.setOnClickListener(btnRestart_Click);
		
		btnRestart_Click.onClick(btnRestart);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}


